package  com.xx.seata.storage;

import javax.sql.DataSource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.pool.DruidDataSource;

import io.seata.rm.datasource.DataSourceProxy;

@SpringBootApplication(scanBasePackages = { " com.xx.seata" })
@EnableDiscoveryClient
@EnableFeignClients
public class StorageApplication {

	public static void main(String[] args) {
		SpringApplication.run(StorageApplication.class, args);
	}

	 @Bean
	    @ConfigurationProperties(prefix = "spring.datasource")
	    public DataSource dataSource() {
	        DruidDataSource druidDataSource = new DruidDataSource();
	        return druidDataSource;
	    }

	    @Primary
	    @Bean("dataSourceProxy")
	    public DataSourceProxy dataSourceProxy(DataSource dataSource) {
	        return new DataSourceProxy(dataSource);
	    }

	    @Bean("jdbcTemplate")
	    @ConditionalOnBean(DataSourceProxy.class)
	    public JdbcTemplate jdbcTemplate(DataSourceProxy dataSourceProxy) {
	        return new JdbcTemplate(dataSourceProxy);
	    }
}

